﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTUDW_CNTN2010_F_CDT.Models;
using PTUDW_CNTN2010_F_CDT.Filters;

namespace PTUDW_CNTN2010_F_CDT.Controllers
{
    [LanguageFilter]
    public class CustomerController : Controller
    {
        private EcommerceDBContext db = new EcommerceDBContext();

        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View(db.Customers.ToList());
        }

        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id = 0)
        {
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        public ActionResult CustomerDetail(string UserName)
        {

            Customer me = db.Customers.Find(UserName);
            if (me == null)
                return HttpNotFound();
            ViewBag.Provinces = new SelectList(db.Provinces, "ProvinceId", "ProvinceName");
            return View(me);
        }

        [HttpPost]
        public ActionResult CustomerDetail(Customer me)
        {
            if (ModelState.IsValid)
            {
               
                db.Entry(me).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("../Home/Index");
            }
            return View(me);
        }
        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                db.Customers.Add(customer);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customer);
        }

        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(Customer customer)
        {
            if (ModelState.IsValid)
            {
                db.Entry(customer).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customer);
        }

        //t
        // GET: /Customer/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        public ActionResult ChangePassword(string UserName)
        {
            ViewBag.UserName = UserName;
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel registerModel, string UserName)
        {
            if (ModelState.IsValid)
            {
                Customer me = db.Customers.Find(UserName);
                if (me == null)
                {
                    ModelState.AddModelError(string.Empty, "Error");
                }
                if (me.Password == registerModel.OldPassword)
                {
                    me.Password = registerModel.ConfirmPassword;
                    db.Entry(me).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("CustomerDetail", new { UserName = UserName });
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Error");
                }
            }
            return RedirectToAction("ChangePassword", new { UserName = UserName });
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Customer customer = db.Customers.Find(id);
            db.Customers.Remove(customer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

       
        
    }
}